package com.weijianhuawen.wjblog.web.markdown.renderer;

import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.commonmark.node.Link;
import org.commonmark.node.Node;
import org.commonmark.node.Text;
import org.commonmark.renderer.NodeRenderer;
import org.commonmark.renderer.html.HtmlNodeRendererContext;
import org.commonmark.renderer.html.HtmlWriter;

import java.util.Objects;
import java.util.Set;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-11-06 14:50
 */
public class LinkNodeRenderer implements NodeRenderer {
    private final HtmlWriter html;
    // 网站域名
    private final static String DOMAIN = "www.weijianhuawen.com";
    public LinkNodeRenderer(HtmlNodeRendererContext context) {
        this.html = context.getWriter();
    }

    @Override
    @SuppressWarnings("unchecked")
    public Set<Class<? extends Node>> getNodeTypes() {
        // 指定自定义渲染节点
        return Sets.newHashSet(Link.class);
    }

    @Override
    public void render(Node node) {
        // 渲染link
        if (node instanceof Link) {
            Link link = (Link) node;

            // 链接内容
            String linkDescription = null;
            if (Objects.nonNull(link.getFirstChild())) {
                Text text = (Text) link.getFirstChild();
                linkDescription = text.getLiteral();
            }
            // 链接url
            String linkUrl = link.getDestination();
            String linkTitle = link.getTitle();
            // 拼接html
            StringBuilder stringBuilder = new StringBuilder("<a href=\"");
            stringBuilder.append(linkUrl);
            stringBuilder.append("\"");
            // 添加title属性
            if (StringUtils.isNotBlank(linkTitle)) {
                stringBuilder.append(" title=\"");
                stringBuilder.append(linkTitle);
                stringBuilder.append("\"");
            }
            // 如果链接不是自己的域名，则添加rel = nofollow
            if (!linkUrl.contains(DOMAIN)) {
                stringBuilder.append(" ref=\"nofollow\"");
            }
            // 添加target= _blank属性
            stringBuilder.append(" target\"_blank\" >");
            // 超链接展示内容
            stringBuilder.append(StringUtils.isNotBlank(linkDescription) ? linkDescription : linkUrl);
            stringBuilder.append("</a>");

            // 添加svg图标
            String svg = "<svg t=\"1730878149463\" class=\"icon w-4 h-4 inline ml-1\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"24262\" width=\"200\" height=\"200\"><path d=\"M191.430914 682.032558l107.574041-124.391055\" fill=\"#FF9292\" p-id=\"24263\"></path><path d=\"M203.064882 692.089611l-23.269983-20.117176 107.568924-124.392078 23.269983 20.118199z\" fill=\"#885F44\" p-id=\"24264\"></path><path d=\"M395.618363 468.419436l230.585679-110.732987\" fill=\"#FF9292\" p-id=\"24265\"></path><path d=\"M402.27497 482.272955l-13.301958-27.714202 230.587726-110.728894 13.300934 27.714202z\" fill=\"#885F44\" p-id=\"24266\"></path><path d=\"M735.270063 380.407939l99.404987 130.402979\" fill=\"#FF9292\" p-id=\"24267\"></path><path d=\"M822.448588 520.136308l-99.402941-130.404003 24.441669-18.646685 99.401917 130.405025z\" fill=\"#885F44\" p-id=\"24268\"></path><path d=\"M353.537965 503.108492m-76.867691 0a76.867692 76.867692 0 1 0 153.735383 0 76.867692 76.867692 0 1 0-153.735383 0Z\" fill=\"#FF9292\" p-id=\"24269\"></path><path d=\"M353.542058 595.352383c-50.864458 0-92.240821-41.376363-92.24082-92.240821s41.376363-92.240821 92.24082-92.24082 92.240821 41.376363 92.240821 92.24082-41.376363 92.240821-92.240821 92.240821z m0-153.73436c-33.914414 0-61.493539 27.586288-61.493539 61.493539s27.579125 61.493539 61.493539 61.49354 61.493539-27.586288 61.49354-61.49354-27.579125-61.493539-61.49354-61.493539z\" fill=\"#885F44\" p-id=\"24270\"></path><path d=\"M680.737052 357.686449m-76.867691 0a76.867692 76.867692 0 1 0 153.735383 0 76.867692 76.867692 0 1 0-153.735383 0Z\" fill=\"#FF9292\" p-id=\"24271\"></path><path d=\"M680.739099 449.927269c-50.864458 0-92.240821-41.376363-92.240821-92.24082s41.376363-92.240821 92.240821-92.240821 92.240821 41.376363 92.240821 92.240821-41.376363 92.240821-92.240821 92.24082z m0-153.73436c-33.914414 0-61.493539 27.586288-61.493539 61.49354s27.579125 61.493539 61.493539 61.493539 61.493539-27.586288 61.493539-61.493539-27.579125-61.493539-61.493539-61.49354z\" fill=\"#885F44\" p-id=\"24272\"></path><path d=\"M862.514095 557.641503m-76.867691 0a76.867692 76.867692 0 1 0 153.735383 0 76.867692 76.867692 0 1 0-153.735383 0Z\" fill=\"#FF9292\" p-id=\"24273\"></path><path d=\"M862.518189 649.880277c-50.864458 0-92.240821-41.376363-92.240821-92.240821s41.376363-92.240821 92.240821-92.24082 92.240821 41.376363 92.24082 92.24082-41.376363 92.240821-92.24082 92.240821z m0-153.73436c-33.914414 0-61.493539 27.586288-61.49354 61.493539s27.579125 61.493539 61.49354 61.49354 61.493539-27.586288 61.493539-61.49354-27.578102-61.493539-61.493539-61.493539z\" fill=\"#885F44\" p-id=\"24274\"></path><path d=\"M153.582911 721.240535m-76.867692 0a76.867692 76.867692 0 1 0 153.735383 0 76.867692 76.867692 0 1 0-153.735383 0Z\" fill=\"#FF9292\" p-id=\"24275\"></path><path d=\"M153.581887 813.479309c-50.864458 0-92.240821-41.376363-92.24082-92.240821s41.376363-92.240821 92.24082-92.24082 92.240821 41.376363 92.240821 92.24082-41.376363 92.240821-92.240821 92.240821z m0-153.735383c-33.914414 0-61.493539 27.586288-61.493539 61.493539s27.579125 61.493539 61.493539 61.493539 61.493539-27.586288 61.49354-61.493539-27.579125-61.493539-61.49354-61.493539z\" fill=\"#885F44\" p-id=\"24276\"></path></svg>";
            stringBuilder.append(svg);
            // 设置html内容
            html.raw(stringBuilder.toString());
        }
    }
}
